var redis = require("redis");
/*QingCloud*/
//RDS_PORT=6379,
//RDS_HOST="192.168.1.234",
//RDS_OPTS={},
//client = redis.createClient(RDS_PORT, RDS_HOST,RDS_OPTS);
/*LeanCache*/
if(process.env['REDIS_URL_zhixingli']==undefined){
 // if(process.env['REDIS_URL_logistics']){
	/**
	 * 添加redis
	 */
	function addRedisContent(key,content,callback){
		return callback("测试环境不支持读取redis");
	};
	/**
	 * 读取redis
	 */
	function getRedisContent(key,callback){
		return callback("测试环境不支持读取redis");
	};
	/**
	 * 删除redis
	 */
	function delRedisContent(key,callback){
		return callback("测试环境不支持读取redis");
	};
}
else{
	// client = redis.createClient(process.env['REDIS_URL_logistics']);
    client = redis.createClient(process.env['REDIS_URL_zhixingli']);
	// client = redis.createClient();
	console.log("redis连接成功");
	//client.on('error', function(err) {
	//	return console.error('redis err: %s', err);
	//});
	/**
	 * 添加redis
	 */
	function addRedisContent(key,content,sec,callback){
		client.set(key, JSON.stringify(content), function(err, reply){
			if(sec!=null&&sec!=""&&sec!=undefined){
				client.expire(key,sec);//设置失效时间(秒)
			}
			return callback(err,null);
		});
	};

	/**
	 * 读取redi.s
	 */
	function getRedisContent(key,callback){
		client.get(key, function(err, reply){
			var jsonObj=JSON.parse(reply);
			return callback(err,jsonObj);
		});
	};
	/**
	 * 删除redis
	 */
	function delRedisContent(key,callback){
		client.del(key, function(err, reply){
			return callback(err,null);
		});
	};
	 /**
	  * 检查 是否存在key
	  * @param key
	  * @param callback
	  * @constructor
      */
	 function exists(key,callback) {
		 client.EXISTS(key, function(err, reply){
			 return callback(err,reply);
		 });
	 }
     function watch(key,callback) {
         client.watch(key);

     }
    function multi(callback) {
         // var multi=client.multi();
            return client.multi();
    }
    function quit() {
        client.quit();

    }
    function exec(callback) {
        client.exec(function (err,reply) {
            return callback(err,reply);
        });
    }
	/**
	 * 将字符串值 value 关联到 key
	 * @param key
	 * @param value
	 * @param sec
	 * @param callback
	 */
	function set(key, value, sec, callback) {
		client.set(key, value, function(err, reply) {
			if(sec != null && sec != "" && sec != undefined) {
				client.expire(key, sec);	//设置失效时间(秒)
			}
			return callback(err, reply);
		});
	}
    function mset(key, value, callback) {
        client.mset(key, JSON.stringify(value), function(err, reply) {
            return callback(err, reply);
        });
    }
    function get(key, callback) {
        client.get(key, function(err, reply) {

            return callback(err, reply);
        });
    }


    /**
	 * 增量操作
	 * @param key 将key的值+1
	 * @param callback
	 * @constructor
	 */
	function incr(key, callback) {
		client.incr(key, function(err, reply) {
			var jsonObj = JSON.parse(reply);
			return callback(err, jsonObj);
		});
	}

	/**
	 * 将一个 member 元素加入到集合 key 当中
	 * @param key
	 * @param value
	 * @param callback
	 */
	function sadd(key, value, callback) {
		client.sadd(key, value, function(err, reply) {
			return callback(err, reply);
		});
	}

	/**
	 * 将一个 member 元素 集合 key 当中  移除
	 * @param key
	 * @param value
	 * @param callback
	 */
	function srem(key, value, callback) {
		client.srem(key, value, function(err, reply) {
			return callback(err, reply);
		});
	}

	/**
	 * 移除并返回集合中的一个随机元素
	 * @param key
	 * @param callback
	 */
	function spop(key, callback) {
		client.spop(key, function(err, reply) {
			var jsonObj = JSON.parse(reply);
			return callback(err, reply);
		});
	}

	/**
	 * 返回集合 key 中的所有成员。
	 * @param key
	 * @param callback
	 */
	function smembers(key, callback) {
		client.smembers(key, function(err, reply) {
			return callback(err, reply);
		});
	}
	/**
	 * 添加修改hash
	 * @param key
	 * @param callback
	 */
	function hmset(key,obj, callback) {
		client.hmset(key,obj, function(err, reply) {
			return callback(err, reply);
		});
	}
	/**
	 * 查询hash
	 * @param key
	 * @param callback
	 */
	function hmget(key,field, callback) {
		client.hmget(key,field, function(err, reply) {
			return callback(err, reply);
		});
	}
	/**
	 * 查询hash所有
	 * @param key
	 * @param callback
	 */
	function hgetall(key, callback) {
		client.hgetall(key, function(err, reply) {
			return callback(err, reply);
		});
	}
	function keys(key, callback) {
		client.keys(key, function(err, reply) {
			return callback(err, reply);
		});
	}



	function zadd(key,score,memb, callback) {
		client.zadd(key,score,memb, function(err, reply) {
			return callback(err, reply);
		});
	}



	function zrevrange (key,start,end, callback) {
		client.zrevrange ([key,start,end], function(err, reply) {
			return callback(err, reply);
		});
	}



	function zscore (key,element, callback) {
		client.zscore (key,element, function(err, reply) {
			return callback(err, reply);
		});
	}
    function zrangebyscore (key,min,max, callback) {
        client.zrangebyscore (key,min,max, function(err, reply) {
            return callback(err, reply);
        });
    }

	function zrem (key,element, callback) {
		client.zrem (key,element, function(err, reply) {
			return callback(err, reply);
		});
	}


	function zrevrank (key,element, callback) {
		client.zrevrank (key,element, function(err, reply) {
			return callback(err, reply);
		});
	}

	/**
	 * Reids  list(列表)相关操作
	 */
	function lpush(key,value,callback) {
		client.LPUSH(key,value,function(err,reply) {
			return callback(err, reply);
		})
	}
    function rpush(key,value,callback) {
        client.rpush(key,value,function(err,reply) {
            return callback(err, reply);
        })
    }
    function rpushx(key,value,callback) {
        client.rpushx(key,value,function(err,reply) {
            return callback(err, reply);
        })
    }
	 function llen(key,callback) {
		 client.llen(key,function(err,reply) {
			 return callback(err, reply);
		 })
	 }
	 function rpop(key,callback) {
		 client.RPOP(key,function(err,reply) {
			 return callback(err, reply);
		 })

	 }

    function lpop(key,callback) {
        client.LPOP(key,function(err,reply) {
            return callback(err, reply);
        })

    }
	 function lrange(key,start,stop,callback) {
		 client.LRANGE(key,start,stop,function(err,reply) {
			 return callback(err, reply);
		 })
	 }
	 function lrem(key,value,callback) {
		 client.LREM (key,0,value,function(err,reply) {
			 return callback(err, reply);
		 })
	 }

    // function sampleTaskMaker(message, func, timeout) {
    //     message = JSON.stringify(message);
    //     console.log("Received a new task:", func, message, "after " + timeout + ".");
    //     redis.multi()
    //         .set(key, content)
    //         .expire(key, timeout)
    //         .exec(function(err) {
    //             if(err) {
    //                 console.error("Failed to publish EXPIRE EVENT for " + content);
    //                 console.error(err);
    //                 return;
    //             }
    //         });
    // };
    // function initRedisSubscribe(callback) {
    //     var  subscriberClient = redis.createClient();
    //     subscriberClient.psubscribe('__keyevent@' + 1 +'__:expired',function () {
    //         console.log('[service] redis start');
    //     });
    //     subscriberClient.on("pmessage", function (pattern, channel, expiredKey) {
    //
    //         // var taskname = expiredKey;
    //
    //        console.log('过期了');
    //
    //     });
    //     callback();
    // }
    //
    // function setProductTask(key) {
		// console.log('5s');
    //     var  schedQueueClient = redis.createClient( );
    //         schedQueueClient.PSETEX(key,5000, '');
    // }



}
exports.delRedisContent=delRedisContent;
exports.addRedisContent=addRedisContent;
exports.getRedisContent=getRedisContent;
exports.keys = keys;
exports.set = set;
exports.mset = mset;
exports.hmset = hmset;
exports.hmget = hmget;
exports.hgetall = hgetall;
exports.incr = incr;
exports.sadd = sadd;
exports.get = get;
exports.srem = srem;
exports.spop = spop;
exports.smembers = smembers;
exports.zadd = zadd;
exports.zrevrange = zrevrange;
exports.zscore = zscore;
exports.zrem = zrem;
exports.zrevrank = zrevrank;
exports.lpush = lpush;
exports.rpush = rpush;
exports.rpushx = rpushx;
exports.llen = llen;
exports.watch = watch;
exports.multi = multi;
exports.exec = exec;
exports.rpop = rpop;
exports.lpop = lpop;
exports.lrange = lrange;
exports.lrem = lrem;
exports.zrangebyscore = zrangebyscore;
exports.exists = exists;
exports.quit = quit;
// exports.initRedisSubscribe=initRedisSubscribe;
// exports.setProductTask=setProductTask;